Skip to content

TYP: annotate functions that always error with NoReturn #48002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2022
Merged

TYP: annotate functions that always error with NoReturn #48002

merged 1 commit into from
Aug 8, 2022

Conversation

twoertwein
Copy link
Member

There are more non-abstract functions that also error. Most of them should probably be abstract functions. I ignored those functions.

@@ -1243,7 +1244,7 @@ def groupings(self) -> list[grouper.Grouping]:
ping = grouper.Grouping(lev, lev, in_axis=False, level=None)
return [ping]

def _aggregate_series_fast(self, obj: Series, func: Callable) -> np.ndarray:
def _aggregate_series_fast(self, obj: Series, func: Callable) -> NoReturn:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we have NotImplementedError, does the NoReturn not create issues with subtypes? (in general, no checked this particular case)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would create issues with sub-types that actually implement these functions. Parent methods should probably be abstract with a signature that the child will implement.

It should be safe adding NoReturn to final methods as no child class is meant to implement them. Based on the doc-string of _aggregate_series_fast I assume this method is basically deprecated and is not implemented in sub-classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. yes it does appear that the exception is not for public consumption.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NoReturn is useful in cases like this to narrow the type:

def error() -> NoReturn: ...

def test(x: int | str) -> str:
    # x: int | str
    if isinstance(x, int):
        error()
    # x: str
    return x + "some_string"

I don't there are many places where NoReturn can be used in pandas, in most cases we should make classes abstract.

Is there a historic reason to avoid inheriting from ABC and using abstractmethod? Happy to make PRs to make more methods abstract.

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @twoertwein lgtm apart from general question on generic usage of NoReturn

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Aug 8, 2022
@simonjayhawkins simonjayhawkins added this to the 1.5 milestone Aug 8, 2022
@mroeschke mroeschke merged commit 6ba2a67 into pandas-dev:main Aug 8, 2022
@mroeschke
Copy link
Member

Thanks @twoertwein

@twoertwein twoertwein deleted the noreturn branch September 10, 2022 01:38
noatamir pushed a commit to noatamir/pandas that referenced this pull request Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants